/*
 * DesktopToolkit.java
 *
 * Copyright 2007 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Shards.
 *
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.shards.toolkit;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.JComponent;

/**
 * Class have some static methods for different operations. Check it out, may be you'll find something useful?
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class DesktopToolkit {
    
    private static final DisplayMode currentMode =  GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
    
    /**
     * Calculating <code>Point</code> of screen center. Gets current display mode from graphics environment, than dividing it.
     * Yes, just few lines of code, but long lines...
     * @return <code>Point</code> of screen center.
     */
    public static Point getScreenCenter() {
        return new Point(currentMode.getWidth()/2, currentMode.getHeight()/2);
    }
    
    /**
     * Calculating <code>Point</code> to place your window (dialog) EXACTLY in screen center.
     * Usage:
     * <code>
     * dialog.pack();
     * dialog.setLocation(DesktopToolkit.getWindowLocation(dialog.getSize()));
     * dialog.setVisible(true);
     * </code>
     * @param size getSize() result from your window
     * @return <code>Point</code> to place your window.
     */
    public static Point getWindowLocation(Dimension size) {
        Point screenCenter = getScreenCenter();
        int x = (screenCenter.x - size.width/2);
        int y = (screenCenter.y - size.height/2);
        return new Point(x, y);
    }
    
    /**
     * Setting maximum component height to preferred. Since Swing have strange 
     * behavior of JTextField and some other components - this method helps to keep 
     * components height in place.
     * @param comp component to set its size. Components needs to have 
     * <code>getPreferredSize()</code> and <code>setMaximumSize()</code> methods.
     */
    public static void setMaxComponentHeight(JComponent comp) {
        comp.setMaximumSize(new Dimension(Short.MAX_VALUE, comp.getPreferredSize().height));
    }
    
}
